<?php
namespace App\Repos;

use App\Models\Task;
use App\Models\User;
use App\Models\TaskProcess;
use App\Models\Depart;
use App\Models\Worktype;
use App\Models\Category;
use DB,Log;
use App\Services\Wechat;

class TaskRepo
{
    // 工单完成
    public static function complete($data)
    {
        $rs['status'] = true;
        try {
            DB::beginTransaction();
            $task = Task::where('id',$data['task_id'])->first();
            $task->status = 100;
            $task->save();
            $depart = Depart::where(['id'=>$task->depart_id])->first();
            $process = TaskProcess::select(['task_id','worker_id','worker_openid'])
                                    ->where(['task_id'=>$data['task_id']])
                                    ->first();
            $work_pic = rtrim(array_get($data,'pics'),',');                       
            if($process) {
                $proData = $process->toArray();                
                $proData['work_pic'] = $work_pic;
                $proData['status'] = 100;
                TaskProcess::create($proData);
            } else {
                $proData['task_id']   = $task->id;
                $proData['worker_id'] = $depart->worker_id;
                $proData['remark']    = $data['remark'];
                $proData['work_pic']  = $work_pic;
                $proData['status'] = 100;
                TaskProcess::create($proData);
            }
            
        } catch (\Exception $e) {
           $rs['status'] = false;
           $rs['msg'] = $e->getMessage();
           DB::rollBack(); 
        }
        DB::commit();

        $msg = $task->toArray();
        $msg['first'] = '工单已完成';
        $msg['address'] = object_get($task,'depart.name').$task->gatenum;
        $msg['cate_name']   = object_get($task,'category.name');
        $msg['worker_name'] = object_get($task,'worker.truename');;
        $msg['time'] = date('Y-m-d H:i:s');
        $msg['url']    = config('app.url').'user/task/show/'.$task->id;
        $msg['remark'] = '点击查看详情';
        self::sendCompleteMsg($msg);
        return $rs;

    }
	
    public static function sendNewMsg($data)
    {
        
        // 先给提交的用户回执
        $msg = $data;
        $msg['first']      = '信息受理成功通知';
        $msg['status_txt'] = '受理成功';
        if($data['category_id']==1) {
            $msg['result']     = '等待处理结果';
        } else {
            $msg['result']     = '已经成功推送信息给相关负责人';
        }
        
        $msg['url']    = config('app.url').'user/task/show/'.$data['id'];
        $msg['remark'] = '';
        Wechat::sendMsgTpl('new_to_user',$msg);

        
           
        
        
        $system = DB::table('systems')->first();

        if(object_get($system,'work_time')) {
            $work_time = explode('-', $system->work_time);
            $isworktime = self::checkIsBetweenTime($work_time[0],$work_time[1]);

            // 非工作时间自动派单
            if(!$isworktime) {
                $task = Task::find($data['id']);
                $msg['first'] = '有新的工单等待处理';
                $cate = Category::where(['id'=>$data['category_id']])->first();         
               
               
                if(object_get($cate,'worker.openid')) {             
                    $msg['openid'] = object_get($cate,'worker.openid');
                    $msg['cate_name'] = $cate->name;
                    $msg['address'] = object_get($task,'depart.name').','.$msg['gatenum'].','.$task->truename.','.$task->mobile;
                    Wechat::sendMsgTpl('dispatch_to_worker',$msg);

                    
                    $task->worker_id = object_get($cate,'worker.id');
                    $task->save();
                }    
            }  else {
                $mangers = User::where('role_code','999')->get();
                foreach($mangers as $manger) {
                    if(empty($manger->openid)) continue;
                    $m = $msg;
                    $m['first']  = '有新的工单等待处理';
                    $m['openid'] = $manger->openid;
                    Wechat::sendMsgTpl('new_to_manager',$m);     
                }     
            }        
        } else {
            $mangers = User::where('role_code','999')->get();
            foreach($mangers as $manger) {
                if(empty($manger->openid)) continue;
                $m = $msg;
                $m['first']  = '有新的工单等待处理';
                $m['openid'] = $manger->openid;
                Wechat::sendMsgTpl('new_to_manager',$m);     
            } 
        } 
    }

    /**
     * 推送完工模板
     */
    public static function sendCompleteMsg($data)
    {
        // 推送消息给提交者     

        Wechat::sendMsgTpl('complete',$data);  // 先推送给客户
        
        $msg = $data;
        // 推送给管理员
        $mangers = User::where(['role_code'=>'999'])->get();
        foreach($mangers as $m) {
            if($m->openid) {
                $msg['openid'] = $m->openid;
                Wechat::sendMsgTpl('complete',$msg);
            }     
        }
        
    }

    public static function sendDispatchMsg($msg)
    {      
        $msg['first'] = '有新的工单等待处理';
        $msg['url']    = config('app.url').'user/task/show/'.$msg['id'];
        $msg['remark'] = '点击查看详情并处理!';
        Wechat::sendMsgTpl('dispatch_to_worker',$msg);  // 先推送给客户
    }


    private static  function checkIsBetweenTime($start,$end){
        $result  = false;
        $date    = date('H:i');
        $curTime = strtotime($date);//当前时分
        $assignTime1 = strtotime($start);//获得指定分钟时间戳，00:00
        $assignTime2 = strtotime($end);//获得指定分钟时间戳，01:00
        
        if($curTime>$assignTime1&&$curTime<$assignTime2){
            $result = true;
        }
        return $result;
    }
	

    


    

	
	
}